#!/bin/bash
#name:run
#description:SeaGameServer run shell

ulimit -SHn 65535
BIN_PATH=`pwd`
echo BIN_PATH=$BIN_PATH

BIN_PATH_LEN=${#BIN_PATH}
BIN_PATH_LEN=$(expr $BIN_PATH_LEN - 4)

APP_HOME=${BIN_PATH:0:BIN_PATH_LEN}
echo APP_HOME=$APP_HOME

SERVER_NAME=$BIN_PATH/run.sh
echo SERVER_NAME=$SERVER_NAME

PATH=${PATH}

STDOUT=$APP_HOME/log/stdout.log
STDERR=$APP_HOME/log/stderr.log

JVM_OPTS="
        -ea \
        -Xms1024m \
        -Xmx4096m \
        -XX:PermSize=128m \
        -XX:MaxNewSize=512m \
        -XX:MaxPermSize=256m \
        -XX:-OmitStackTraceInFastThrow \
        -XX:+UseCompressedOops \
        -XX:+UseParNewGC \
        -XX:+UseConcMarkSweepGC \
        -XX:+CMSParallelRemarkEnabled \
        -XX:SurvivorRatio=8 \
        -XX:MaxTenuringThreshold=1 \
        -XX:CMSInitiatingOccupancyFraction=75 \
        -XX:+UseCMSInitiatingOccupancyOnly \
        -XX:+HeapDumpOnOutOfMemoryError \
        -Xloggc:gc.$$ -verbose:gc -XX:+PrintGCDetails"
        #-Dcom.sun.management.jmxremote \
        #-Dcom.sun.management.jmxremote.port=8081 \
        #-Dcom.sun.management.jmxremote.ssl=false \
        #-Dcom.sun.management.jmxremote.authenticate=false"

ARGS=
APPID=test01

CLASS_NAME=com.gfan.gameserver.main.SeaGameServerMain
PID=$BIN_PATH/instance.pid

LIB=$APP_HOME/lib

LOG=$APP_HOME/log/general.log

JARS=(`ls ../lib`)
DIRNUM=${#JARS[@]}

CLASS_PATH=$APP_HOME/classes/

index=0
while [ $index -lt $DIRNUM ]
do
  CLASS_PATH=$CLASS_PATH:$LIB/${JARS[$index]}
  let "index= $index + 1"
done

#export LANG=zh_CN
#export LC_ALL=zh_CN

#=============================================================================
start()
{
echo 'to start server at ' `date` >> $LOG
if test -e $PID
        then
                echo
                echo The $SERVER_NAME Server already Started!
                echo
        else
                echo
                echo Start The $SERVER_NAME Server....
				
            #java $JVM_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,address=1236,server=y,suspend=n -D$APPID -classpath $CLASS_PATH $CLASS_NAME $ARGS >>$STDOUT 2>>$STDERR &
            java $JVM_OPTS -DAppID=$APPID -classpath $CLASS_PATH $CLASS_NAME $ARGS >>$STDOUT 2>>$STDERR &
                echo $!>$PID
                sleep 2
                STATUS=`ps -p $!|grep java |awk '{print $1}'`
                if test $STATUS
                        then
                                echo The $SERVER_NAME Server Started!
                                echo
                        else
                                rm $PID
				echo The $SERVER_NAME Server Start Failed
                                echo please Check the system
                                echo
                fi
fi
}

stop()
{
echo 'to stop server at ' `date` >> $LOG
if test -e $PID
        then    
                echo
                echo Stop The $SERVER_NAME Server....
                echo
                TPID=`cat $PID`
                kill $TPID
                sleep 5
                STATUS=`ps -p $TPID |grep java | awk '{print $1}'`
                if test $STATUS
                        then
                                echo The $SERVER_NAME Server NOT Stoped!
                                echo please Check the system
                                echo
                        else
                                echo The $SERVER_NAME Server Stoped
                                echo
                                rm $PID
                fi
        else
                echo
                echo The $SERVER_NAME Server already Stoped!
                echo
fi
}

status()
{
echo
if test -e $PID
        then
                TPID=`cat $PID`
                STATUS=`ps -p $TPID|grep java | awk '{print $1}'`
                if test $STATUS
                        then
                                echo "The $SERVER_NAME Server Running($TPID)!"
                                echo
                        else
                                echo The $SERVER_NAME Server NOT Running!
                                rm $PID
                                echo
                fi
        else
                echo
                echo The $SERVER_NAME Server NOT Running!
                echo
fi
}

case "$1" in
'start')
                start
        ;;
'stop')
                stop
        ;;
'restart')
		stop
		start
	;;
'status')
                status
        ;;
*)
        echo
        echo
        echo "Usage: $0 {status | start | stop }"
        echo
        echo Status of $SERVER_NAME Servers ......
                status
        ;;
esac
exit 0

